<template>
  <div class="switch" :class="{'checked-default': value && !size, 'checked-large': value && size === 'large', 'checked-middle': value && size === 'middle'}">
    <span class="switch-core" ref="core" :class="size">
      <span class="switch-btn"></span>
    </span>
    <input class="switch-input" :disabled="disabled" @click="changeSwitch" :class="{'middle': size === 'middle','large': size === 'large', 'disabled': disabled}">
  </div>
</template>
<script>
export default {
  name: 's-switch',
  props: {
    value: {
      type: Boolean,
      default: false
    },
    activeColor: {
      type: String,
      default: '#409eff'
    },
    inactiveColor: {
      type: String,
      default: '#dcdfe6'
    },
    size: {
      type: String,
      default: ''
    },
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {}
  },
  methods: {
    changeSwitch () {
      this.$emit('input', !this.value)
      this.$emit('click')
    },
    setColor () {
      if (this.activeColor || this.inactiveColor) {
        const color = this.value ? this.activeColor : this.inactiveColor
        this.$refs.core.style.backgroundColor = color
        this.$refs.core.style.borderColor = color
      }
    }
  },
  mounted () {
    this.setColor()
  },
  updated () {
    this.setColor()
  }
}
</script>
<style lang='scss' scoped>
.switch {
  display: inline-block;
  align-items: center;
  position: relative;
  font-size: 14px;
  vertical-align: middle;
  margin: 0 4px 0 4px;
  .switch-input {
    width: 36px;
    height: 18.5px;
    position: absolute;
    top: 1px;
    left: 0;
    border-radius: 10px;
    opacity: 0;
    outline: none;
    cursor: pointer;
    &.disabled {
      cursor: not-allowed;
      background-color: #dadada;
      opacity: 0.6;
      border: 0px;
    }
    &.middle {
      width: 46px;
      height: 24px;
      border-radius: 18px;
    }
    &.large {
      width: 66px;
      height: 28px;
      border-radius: 20px;
    }
  }
  .switch-core {
    margin: 0;
    display: inline-block;
    position: relative;
    border: 1px solid #dcdfe6;
    outline: none;
    width: 40px;
    height: 20px;
    border-radius: 10px;
    box-sizing: border-box;
    background: #dcdfe6;
    transition: border-color .3s background-color .3s;
    vertical-align: middle;
    &.middle {
      width: 50px;
      height: 26px;
      border-radius: 18px;
      transform: translateY(1px);
      .switch-btn {
        width: 20px;
        height: 20px;
        position: absolute;
        top: 2px;
        left: 1px;
      }
    }
    &.large {
      width: 70px;
      height: 30px;
      border-radius: 20px;
      transform: translateY(1px);
      .switch-btn {
        width: 24px;
        height: 24px;
        position: absolute;
        top: 2px;
        left: 2px;
      }
    }
    .switch-btn {
      position: absolute;
      top: 1px;
      left: 1px;
      border-radius: 100%;
      transition: all .3s;
      width: 16px;
      height: 16px;
      background-color: #fff;
    }
  }
  // 点击切换时的样式
  &.checked-default {
    .switch-btn {
      transform: translateX(20px);
    }
  }
  &.checked-middle {
    .switch-btn {
      transform: translateX(26px);
    }
  }
  &.checked-large {
    .switch-btn {
      transform: translateX(41px);
    }
  }
}
</style>
